local function_list = _G.ServerOHOH.function_list
local M = _G.ServerOHOH.class.new(script,_G.ServerOHOH.files.Server_PlayerComponent(),script.Parent.Name);
local ServerOHOH = _G.ServerOHOH;
local Rebirth_Cfg=ServerOHOH.config.Rebirth
local ItemTool=_G.CommonOHOH.load.ItemTool()
local Tool=require(script.Parent.Tool)

function M.new(self,...)
    M(self,...);
end

function M.ctor(self,parent)
    M.super.ctor(self,parent)
    self.player = self.parent
    self.DataStore = self.parent:GetComponent(_G.ServerOHOH.load.Server_PlayerDataComponent());
    self.ItemCtrl = self.parent:GetComponent(_G.ServerOHOH.load.Server_ItemCtrl())
    
    self.player.RobuxBuyDoneEvent:Connect(function(gid,gtype,...)
        if gtype == "rebirth" then
            self:BuyDone(gid,gtype,...)
        end
    end)

    Tool.AddBoost(self.player,self.DataStore)
end


function M.BuyDone(self)
    if self:isCanRebirth() then
        self:Rebirth()
    end
end

function M.CheckRebirthData(self)

    if not self:IsLevelCanRebirth() then
        return false,0
    end

    if not self:IsHadEnough() then
               --1数量不足
         return false,1
    end

    local key,count=Tool.GetCostItemRebirthtData(self.DataStore)
    if not key then
        return false,2 --找不到数据
     end
    self.ItemCtrl:AddAnyThing(key,-count,false,false)
    self:Rebirth()
end


-- 是否能够重生
function M.isCanRebirth(self)
    return Tool.isCanRebirth(self.DataStore)
end


-- 是否达到重生等级
function M.IsLevelCanRebirth(self)
    return Tool.IsLevelCanRebirth(self.DataStore)
end

-- 是否达到重生等级
function M.IsHadEnough(self)
    return Tool.IsHadEnough(self.DataStore)
end

--重生
function M.Rebirth(self)
    local rebirth = self.DataStore:GetData("base").rebirth
    local cfg= Rebirth_Cfg[rebirth]
    if not cfg then
        return 
    end
    local afterCsumLevel = cfg["afterCsumLevel"]
    local addData = {
        base_rebirth = 1,
    }
    self.ItemCtrl:AddItem(addData,false,false)
    self.parent:GetComponent(_G.CommonOHOH.load.Common_AttributeCtrl()):Rebirth(afterCsumLevel)
	self.parent:SynchDataToClient("base")
   
end

function M.changeAutoRebirch(self)
   if  self.DataStore:GetData("set_data").isAutoRebirth==1 then
        self.DataStore:GetData("set_data").isAutoRebirth=0
   else
        self.DataStore:GetData("set_data").isAutoRebirth=1
   end

   self.DataStore:Execut(function()
    
   end)
   self.parent:SynchDataToClient("set_data")

end



M:RegistFunction(function_list.check_rebirth_data,M.CheckRebirthData)

M:RegistFunction(function_list.rebirth_buy_done,M.BuyDone)

M:RegistFunction(function_list.change_auto_rebirch,M.changeAutoRebirch)


function M.dtor(self)
    M.super.dtor(self)
end

return M